﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using BLL;
using System.Web.Http;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using YQFK.Models;

namespace YQFK.api
{

    /// <summary>
    /// 异常体温登记
    /// </summary>
    public class TemperatureHighController : ApiController
    {

        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        /// <summary>
        /// 根据id获取单条数据
        /// </summary>
        /// <param name="TemperatureId"></param>
        /// <returns></returns>
        //GET api/TemperatureHigh/GetTemperatureHighById?TemperatureId=TemperatureId
        public HttpResponseMessage GetTemperatureHighById(string TemperatureId)
        {
            ResponeModel rm = new ResponeModel();
            rm.code = 0;
            rm.info = "查询成功";
            TemperatureHighModel thm = new TemperatureHighBll().GetSingleDataById(TemperatureId);
            if (thm == null || thm.TemperatureId.ToString() == "00000000-0000-0000-0000-000000000000")
            {
                rm.count = 0;
                rm.data = new object();
                var obj = new { code = 0, count = 0, info = "查询成功", date = new object() };
                var result = JsonConvert.SerializeObject(obj);
                return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") };
            }
            else
            {
                AttachmentModel am = new LoginUserBll().GetAttachmentById(thm.userId);
                EmployeeModel em = new LoginUserBll().GetEmployeeById(thm.userId);
                string imgUrl = "";
                if (am != null && am.AttachmentId.ToString() != "00000000-0000-0000-0000-000000000000")
                {
                    imgUrl = Url.Link("DefaultApi", new { id = am.AttachmentId, controller = "Attachment", action = "Get" });
                }

                rm.data = new
                {
                    BodyCondition = thm.BodyCondition,
                    DealInfo = thm.DealInfo,
                    DepartId = thm.DepartId,
                    DepartmentName = thm.DepartmentName,
                    DepartName = thm.DepartName,
                    RegistDate = thm.RegistDate.ToString("yyyy-MM-dd HH:mm:ss"),
                    Temperature = thm.Temperature,
                    TemperatureId = thm.TemperatureId,
                    userId = thm.userId,
                    userName = thm.userName,
                    post = em.Post,//岗位
                    age = em.Age,
                    imgUrl = imgUrl,
                    Gender=em.Gender
                };
                rm.count = 0;


                var result = JsonConvert.SerializeObject(rm);
                return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") };
            }

        }

        /// <summary>
        /// 获取所有体温数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //GET api/TemperatureHigh/GetAllTH
        public HttpResponseMessage GetAllTH()
        {
            List<TemperatureHighModel> thmLi = new TemperatureHighBll().GetDataList();
            var result = JsonConvert.SerializeObject(thmLi);
            return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        ///获取处理措施
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        //GET api/TemperatureHigh/GetDealInfo
        public HttpResponseMessage GetDealInfo()
        {
            List<string> strLi = new List<string>();
            strLi.Add("上岗");
            strLi.Add("劝返");
            strLi.Add("隔离");
            strLi.Add("送防疫部门");
            var result = JsonConvert.SerializeObject(strLi);
            return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") };
        }

        /// <summary>
        ///获取症状
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        //GET api/TemperatureHigh/GetSymptom
        public HttpResponseMessage GetSymptom()
        {
            List<string> strLi = new List<string>();
            strLi.Add("咳嗽");
            strLi.Add("乏力");
            strLi.Add("发热");
            strLi.Add("腹泻");
            strLi.Add("胸闷");
            strLi.Add("其他");
            var result = JsonConvert.SerializeObject(strLi);
            return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") };
        }


        /// <summary>
        /// 根据departId获取分页数据
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        //POST api/TemperatureHigh/PostDataList
        public HttpResponseMessage PostDataList(JObject value)
        {
            string jsonString = JsonConvert.SerializeObject(value);
            var jsonParams = JsonConvert.DeserializeObject<dynamic>(jsonString);
            string departId = jsonParams.departId;
            string pageIndex = jsonParams.pageIndex;
            string pageSize = jsonParams.pagesize;
            string userName = jsonParams.userName;
            string departName = jsonParams.departName;
            string beginTime = jsonParams.beginTime;
            string endTime = jsonParams.endTime;
            int total = 0;
            List<TemperatureHighModel> thmLi = new TemperatureHighBll().GetDataListByDepartId(departId, departName, userName, int.Parse(pageSize), int.Parse(pageIndex), out total,beginTime,endTime);
            var returnValue = thmLi.Select(c => {
                AttachmentModel am = new LoginUserBll().GetAttachmentById(c.userId);
                EmployeeModel em = new LoginUserBll().GetEmployeeById(c.userId);
                string imgUrl = "";
                if (am != null && am.AttachmentId.ToString() != "00000000-0000-0000-0000-000000000000")
                {
                    imgUrl = Url.Link("DefaultApi", new { id = am.AttachmentId, controller = "Attachment", action = "Get" });
                }

                return new {
                    BodyCondition = c.BodyCondition,

                    DealInfo = c.DealInfo,
                    DepartId = c.DepartId,
                    DepartmentName = c.DepartmentName,
                    DepartName = c.DepartName,
                    RegistDate = c.RegistDate.ToString("yyyy-MM-dd HH:mm:ss"),
                    Temperature = c.Temperature,
                    TemperatureId = c.TemperatureId,
                    userId = c.userId,
                    userName = c.userName,
                    post = em.Post,//岗位
                    age = em.Age,
                    imgUrl = imgUrl,
                    Gender = em.Gender
                };
            });
            ResponeModel rm = new ResponeModel();
            rm.code = 0;
            rm.count = total;
            rm.data = returnValue;
            rm.info = "查询成功";

            var result = JsonConvert.SerializeObject(rm);
            return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") };
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {

        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        /// <summary>
        /// 修改单挑数据
        /// </summary>
        /// <param name="jObject"></param>
        /// <returns></returns>
        // Post api/TemperatureHigh/PostSaveSingleData
        public HttpResponseMessage PostSaveSingleData(JObject jObject)
        {

            string jsonString = JsonConvert.SerializeObject(jObject);
            TemperatureHighModel thm = JsonConvert.DeserializeObject<TemperatureHighModel>(jsonString);
            ResponeModel rm = new ResponeModel();

            rm.count = 0;
            rm.data = new object();
            if (thm.TemperatureId == null || thm.TemperatureId.ToString() == "00000000-0000-0000-0000-000000000000")//新增
            {
                thm.CreateTime = DateTime.Now;
                thm.UpdateTime = DateTime.Now;
                bool b = new TemperatureHighBll().InsertSingleData(thm);
                if (b)
                {
                    rm.code = 0;
                    rm.info = "操作成功";
                }
                else
                {
                    rm.code = -1;
                    rm.info = "操作失败";
                }

            }
            else//修改
            {
                bool b = new TemperatureHighBll().UpdateSingleDataByTempId(thm);
                if (b)
                {
                    rm.code = 0;
                    rm.info = "操作成功";
                }
                else
                {
                    rm.code = -1;
                    rm.info = "操作失败";
                }
            }
            var result = JsonConvert.SerializeObject(rm);
            return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") };
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}